package gxb.test12;

interface Calculator{
    int computer(int n ,int m);
}
class Addition implements Calculator {
    @Override
    public int computer(int n, int m) {
        return n + m;
    }

}

class Subtraction implements Calculator {
    public int computer(int n, int m) {
        return n - m;
    }
}

class Multiplication implements Calculator {
    public int computer(int n, int m) {
        return n * m;
    }
}

class Division implements Calculator {
    public int computer(int n, int m) {
        if (m == 0) {
            throw new IllegalArgumentException("除数不能为0");
        }
        return n / m;
    }
}
class Computer{
    public void useCal(Calculator com ,int op1,int op2){
        int result = com.computer(op1,op2);
        System.out.print(result);
    }
}
public class TestCh09_02 {
    public static void main(String[] args) {
        Computer computer = new Computer();

        int operand1 = 25;
        int operand2 = 6;

        Calculator addition = new Addition();
        System.out.print(operand1+"+"+operand2+"和为：");
        computer.useCal(addition, operand1, operand2); // 执行加法运算
        System.out.println();

        operand1=32;
        operand2=12;
        Calculator subtraction = new Subtraction();
        System.out.print(operand1+"-"+operand2+"差为：");
        computer.useCal(subtraction, operand1, operand2); // 执行减法运算
        System.out.println();

        operand1=15;
        operand2=5;
        Calculator multiplication = new Multiplication();
        System.out.print(operand1+"*"+operand2+"乘积为：");
        computer.useCal(multiplication, operand1, operand2); // 执行乘法运算
        System.out.println();

        operand1=16;
        operand2=2;
        Calculator division = new Division();
        System.out.print(operand1+"/"+operand2+"商为：");
        computer.useCal(division, operand1, operand2); // 执行除法运算
    }
}
